package org.shekhar.jbpm_demo;

import org.hibernate.SessionFactory;
import org.jbpm.api.RepositoryService;
import org.jbpm.pvm.internal.cfg.JbpmConfiguration;
import org.jbpm.test.JbpmTestCase;
import org.junit.Before;
import org.shekhar.jbpm_demo.dao.DAOFactory;
import org.shekhar.jbpm_demo.hibernate.HibernateDAOFactory;
import org.shekhar.jbpm_demo.persistence.HibernateUtil;

/**
 * Created by IntelliJ IDEA.
 * User: svemuri
 * Date: Jul 17, 2009
 * Time: 6:06:04 PM
 * @TODO: clean up so that there is no copy pasting.
 */

public abstract class AbstractJbpmDemoTestCase extends JbpmTestCase {

    SessionFactory sessionFactory;
    DAOFactory daoFactory;

    static {
        System.setProperty("jbpm.test.cfg.resource", "jbpm_demo.jbpm.cfg.xml");
    }

    @Before
    public void setUp() throws Exception {
        sessionFactory = HibernateUtil.getSessionFactory();

        daoFactory = DAOFactory.instance(HibernateDAOFactory.class);

        initialize();

    }

    protected synchronized void initialize() {
        if (processEngine == null) {

            JbpmConfiguration configuration = new JbpmConfiguration();

            String jbpmTestCfgResource = System.getProperty("jbpm.test.cfg.resource");
            if (jbpmTestCfgResource != null) {
                configuration.setResource(jbpmTestCfgResource);
                configuration.setSessionFactory(sessionFactory);
            }

            processEngine = configuration.buildProcessEngine();

            log.debug("using ProcessEngine " + System.identityHashCode(processEngine));

            repositoryService = processEngine.get(RepositoryService.class);
            executionService = processEngine.getExecutionService();
            historyService = processEngine.getHistoryService();
            managementService = processEngine.getManagementService();
            taskService = processEngine.getTaskService();
            identityService = processEngine.getIdentityService();
        }
    }
}
